package cn.neusoft.config;


import cn.neusoft.interceptor.Loginterceptor;
import cn.neusoft.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
	//解决跨域问题
	@Value("${alloworigins}")
	String[] allowOrigins;
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		System.out.println("允许跨域路径：");
		System.out.println(Arrays.toString(allowOrigins));
		registry.addMapping("/**").
				allowedOrigins(allowOrigins)
				.allowCredentials(true)
				.allowedMethods("GET", "POST","PUT","DELETE","OPTIONS")
				.allowedHeaders("*")
				.maxAge(36000);
	}
	@Value("${uploadlocation}")
	String uploadlocation;
	//根据我们访问文件资源进行一个映射，
	//映射到文件存储的地方，也就是d盘的upload1118这个目录下
	/**
	 * 相当于 springmvc.xml中配置的
	 * <mvc:resources mapping="/img/**" location="file:D://upload1118/"/>
	 * @param registry
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		System.out.println("上传"+uploadlocation);
		registry.addResourceHandler("/img/**")
				.addResourceLocations("file:///"+uploadlocation);
	}
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		System.out.println("拦截器");
//		registry.addInterceptor(new Loginterceptor())
//				.addPathPatterns("/**")
//				.excludePathPatterns("/login/in");
		registry.addInterceptor(new MyInterceptor())
				.addPathPatterns("/**");

	}
}
